import sys
import pygame


# 玩家对象
class Player(object):

	def __init__(self):

		self.image=pygame.image.load('Pics/player.png').convert_alpha();
		# 加载对象贴图
		self.rect = self.image.get_rect();
		# 获取图像坐标
		self.map=mapTemlate.get("mid");
		self.mapTemlateName="mid";
		# 获取地图的模板
		self.x=5;
		# x轴坐标
		self.y=7;
		# y轴坐标


	def update(self):
	# 移动

		tmpX=self.x;
		# 临时的x
		tmpY=self.y;
		# 临时的y

		key = pygame.key.get_pressed();
		# 上下左右移动
		if key[pygame.K_UP] :
			self.y -= 1;
			print("向上移动")
		elif key[pygame.K_DOWN] :
			self.y += 1;
			print("向下移动")
		elif key[pygame.K_LEFT] :
			self.x -= 1;
			print("向左移动")
		elif key[pygame.K_RIGHT] :
			self.x += 1;
			print("向右移动")

		print(self.y, self.x);
		if (self.x >= 0 and self.x < 12) and (self.y >= 0 and self.y < 12):

			if self.map[self.y][self.x] == 0 :
				self.x=tmpX;
				self.y=tmpY;


		self.rect.x = self.x * 50;
		self.rect.y = self.y * 50;
		screen.blit(self.image, self.rect);







class Maps(object):



	def __init__(self):

		self.bg=pygame.image.load("Pics/mid.png").convert_alpha();
		# 加载对象贴图
		self.rect=self.bg.get_rect();
		# 获取图像坐标



class GameMain(object):


	def	__init__(self):
		self.maps = Maps();
		self.play=Player();

	def run(self):

		screen.blit(self.maps.bg,(0,0));
		# 先画地图

		while True:
			clock.tick(60);
			for even in pygame.event.get():
				screen.blit(self.maps.bg, (0, 0));
				self.mapSwitch();
				# 切换地图
				self.play.update();
				# 更新画板
			pygame.display.update()


	def mapSwitch(self):
	# 切换地图
		if self.play.mapTemlateName == "mid":
			if self.play.x >= 12:
				# 设置画板
				self.mapSwitchX("right");
			if self.play.y < 0 :
				self.mapSwitchY("top")

		if self.play.mapTemlateName == "left":
			print();
		if self.play.mapTemlateName == "right":
			if self.play.x < 0 :
				self.mapSwitchX("mid",True)
		if self.play.mapTemlateName == "top":
			if self.play.y >= 12:
				self.mapSwitchY("mid",True);
		if self.play.mapTemlateName == "topRight":
			print();
		if self.play.mapTemlateName == "topLeft" :
			print();


	def mapSwitchX(self,direction,flg=False):
	# 地图切换 X轴归零或最大值
		self.maps.bg = pygame.image.load("Pics/"+direction+".png").convert_alpha();
		# 更换地图贴图
		screen.blit(self.maps.bg, (0, 0));
		if flg == False :
			self.play.x = 0;
		else:
			self.play.x=11;
		# right x轴归零
		# 设置地图贴图
		self.play.map = mapTemlate.get(direction);
		# 设置right的地图信息
		self.play.mapTemlateName = direction;

	def mapSwitchY(self,direction, flg = False):
	# 地图切换 X轴归零或最大值

		self.maps.bg = pygame.image.load("Pics/" + direction + ".png").convert_alpha();
		# 更换地图贴图
		screen.blit(self.maps.bg, (0, 0));
		if flg == False:
			self.play.y = 11;
		else:
			self.play.y = 0;
		#direction  y轴归零
		# 设置地图贴图
		self.play.map = mapTemlate.get(direction);
		# 设置direction的地图信息
		self.play.mapTemlateName = direction;




# 全局属性
pygame.init()
#初始化pygame
screen_width = 600;
#画布宽度
screen_height = 600;
#画布高
screen = pygame.display.set_mode([screen_height, screen_width]);
# 设置画布大小
pygame.display.set_caption('player_control');
# 设置标签名
clock = pygame.time.Clock();
# 跟踪时间
goY = 72;
# 走路速度
goX = 48;
# 走路速度

mapTemlate={
	# 地图
	"mid":[
		# 中间
		[1,1,1,1,1,0,1,0,1,1,1,1],
		[0,0,0,1,1,0,1,0,1,0,0,0],
		[0,0,2,1,1,0,1,0,1,2,0,0],
		[0,0,2,1,1,1,1,0,1,2,0,0],
		[0,0,0,1,1,0,1,0,1,0,0,0],
		[1,1,1,1,1,0,1,0,1,1,1,1],
		[0,0,0,0,0,0,1,0,1,0,0,0],
		[1,1,1,1,1,1,1,1,1,1,1,1],
		[1,1,1,1,1,1,1,1,1,1,1,1],
		[1,1,1,1,1,0,0,0,1,1,1,1],
		[1,1,1,1,1,0,0,0,1,1,1,1],
		[0,0,0,0,0,0,0,0,0,0,0,0]
	],
	"left":[
		# 左
		[0,0,0,0,0,0,0,0,0,0,0,0],
		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 1],
		[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
		[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
	],
	"topRight":[
		# 上右
		[],
		[],
		[],
		[],
		[],
		[],
		[],
		[],
		[],
		[],
		[],
		[]
	],
	"topLeft":[
		# 上左
		[],
		[],
		[],
		[],
		[],
		[],
		[],
		[],
		[],
		[],
		[],
		[]
	]
}



if __name__ == '__main__':
	game= GameMain()
	game.run();

from ditu.ditu_01 import beijing_01_list

renwuX = 5
renwuY = 5

pingmudaxiao = 7


class Renwu(pygame.sprite.Sprite):
	def __init__(self):
		super(Renwu, self).__init__()
		self.renwu_surface = pygame.image.load('Pics/renwu.png').convert_alpha()
		self.dangqianyemian = 0
		# 0主界面 1背包 2战斗
		self.caidanxuanze_surface = pygame.image.load('Pics/main_menu_selection.png').convert_alpha()
		self.beibao_surface = pygame.image.load('Pics/main_menu.png').convert_alpha()
	
	def renwu_draw(self):
		beijing.blit(self.renwu_surface, (300, 300))
	
	def renwudongzuo(self, keys):
		
		global renwuX
		global renwuY
		# 上下左右
		if self.dangqianyemian == 0:
			if keys[pygame.K_LEFT]:
				beijing.fill((0, 0, 0))
				renwuY -= 1
				Ditu().ditu_draw()
			elif keys[pygame.K_RIGHT]:
				beijing.fill((0, 0, 0))
				renwuY += 1
				Ditu().ditu_draw()
			elif keys[pygame.K_UP]:
				beijing.fill((0, 0, 0))
				renwuX -= 1
				Ditu().ditu_draw()
			elif keys[pygame.K_DOWN]:
				beijing.fill((0, 0, 0))
				renwuX += 1
				Ditu().ditu_draw()
			elif keys[pygame.K_b]:
				self.dangqianyemian = 1
				self.renwubeibao_draw()
		
		
		# 背包
		elif self.dangqianyemian == 1:
			if keys[pygame.K_b]:
				self.dangqianyemian = 0
				self.dangqianyemian = 0
				beijing.fill((0, 0, 0))
				Ditu().ditu_draw()
			
			else:
				self.renwubeibao_draw()
		else:
			Ditu().ditu_draw()
		self.renwu_draw()
	
	def renwubeibao_draw(self):
		Ditu().ditu_draw()
		self.beibao_surface.blit(self.caidanxuanze_surface, (0, 0))
		beijing.blit(self.beibao_surface, (200, 200))


class Ditu(pygame.sprite.Sprite):
	def __init__(self):
		super(Ditu, self).__init__()
		self.ditu_surface_1 = pygame.image.load('Pics/1.png').convert_alpha()
		self.ditu_surface_2 = pygame.image.load('Pics/2.png').convert_alpha()

	def ditu_draw(self):
		
		self.tempX = 0
		for i in range(renwuX - 3, renwuX + 4):
			self.tempY = 0
			for p in range(renwuY - 3, renwuY + 4):
				if beijing_01_list[i][p] == 1:
					beijing.blit(self.ditu_surface_1, (self.tempY * 100, self.tempX * 100))
				elif beijing_01_list[i][p] == 2:
					beijing.blit(self.ditu_surface_2, (self.tempY * 100, self.tempX * 100))
				self.tempY += 1
			self.tempX += 1


def main():
	pygame.init()
	global beijing
	beijing = pygame.display.set_mode((pingmudaxiao * 100, pingmudaxiao * 100))
	renwu = Renwu()
	ditu = Ditu()
	ditu.ditu_draw()
	renwu.renwu_draw()
	while True:
		for event in pygame.event.get():
			if event.type == pygame.KEYDOWN:
				keys = pygame.key.get_pressed()
				renwu.renwudongzuo(keys)
			if event.type == pygame.QUIT:
				pygame.quit()
				sys.exit()
		
		pygame.display.flip()

if __name__ == '__main__':
	main()

